﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project.DTO;
using System.Data.Common;
using System.Data;

namespace Project.DAO
{
    public class videoDAO
    {
        //insert new a video
        public static Boolean insertNewAVideo(videoDTO vdn)
        {
            Boolean flag = false;
            DataProvider dp = new DataProvider();

            DbCommand command = dp.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "spInsertAVideo";

            DbParameter parm = command.CreateParameter();
            parm.ParameterName = "@titlevideo";
            parm.DbType = DbType.String;
            parm.Value =vdn.titleVideo;
            command.Parameters.Add(parm);

            DbParameter parm1 = command.CreateParameter();
            parm1.ParameterName = "@uploader1";
            parm1.DbType = DbType.Int32;
            parm1.Value = vdn.uploader;
            command.Parameters.Add(parm1);

            DbParameter parm2 = command.CreateParameter();
            parm2.ParameterName = "@filesize";
            parm2.DbType = DbType.String;
            parm2.Value = vdn.fileSize;
            command.Parameters.Add(parm2);

            DbParameter parm3 = command.CreateParameter();
            parm3.ParameterName = "@uploaddate";
            parm3.DbType = DbType.String;
            parm3.Value = vdn.uploadDate;
            command.Parameters.Add(parm3);

            DbParameter parm4 = command.CreateParameter();
            parm4.ParameterName = "@urlvideo";
            parm4.DbType = DbType.String;
            parm4.Value = vdn.urlVideo;
            command.Parameters.Add(parm4);

            DbParameter parm5 = command.CreateParameter();
            parm5.ParameterName = "@description1";
            parm5.DbType = DbType.String;
            parm5.Value = vdn.description;
            command.Parameters.Add(parm5);

            try
            {
                dp.ExecuteNonQueryCommand(command);
                flag = true;
            }
            catch
            {
                return flag;
            }

           
            return flag;
        }
   
        //get all video upload today
        public static videoDTO[] getAllVideoUploadToday(string date) {
            List<videoDTO> dsVideo = new List<videoDTO>();
            return dsVideo.ToArray<videoDTO>();
        }

        //get all video 
        public static videoDTO[] getAllVideo()
        {
            List<videoDTO> dsVideo = new List<videoDTO>();

            DataProvider dp = new DataProvider();

            DbCommand command = dp.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "spGetAllVideo";

            DataTable dt = dp.ExecuteSelectCommand(command);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                videoDTO vdt = new videoDTO();

                vdt.idVideo = Int32.Parse(dt.Rows[i]["idVideo"].ToString());
                vdt.titleVideo = dt.Rows[i]["titleVideo"].ToString();
                vdt.uploader = Int32.Parse(dt.Rows[i]["uploader"].ToString());
                vdt.urlVideo = dt.Rows[i]["urlVideo"].ToString();
                vdt.fileSize = dt.Rows[i]["fileSize"].ToString();
                vdt.uploadDate = dt.Rows[i]["uploadDate"].ToString();
                vdt.description = dt.Rows[i]["description"].ToString();

                dsVideo.Add(vdt);
            }

            return dsVideo.ToArray<videoDTO>();
        }

        //get a video 
        public static videoDTO getAVideo(int idvideo)
        {
            videoDTO vdt = new videoDTO();
            DataProvider dp = new DataProvider();

            DbCommand command = dp.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "spGetAVideo";

            DbParameter parm = command.CreateParameter();
            parm.ParameterName = "@idvideo";
            parm.DbType = DbType.String;
            parm.Value = idvideo;
            command.Parameters.Add(parm);

            DataTable dt = dp.ExecuteSelectCommand(command);

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                vdt.idVideo = Int32.Parse(dt.Rows[i]["idVideo"].ToString());
                vdt.titleVideo = dt.Rows[i]["titleVideo"].ToString();
                vdt.uploader = Int32.Parse(dt.Rows[i]["uploader"].ToString());
                vdt.urlVideo = dt.Rows[i]["urlVideo"].ToString();
                vdt.fileSize = dt.Rows[i]["fileSize"].ToString();
                vdt.uploadDate = dt.Rows[i]["uploadDate"].ToString();
                vdt.description = dt.Rows[i]["description"].ToString();
                
            }

            return vdt;
        }

    }
}